﻿using System;
using System.Collections.Generic;
using System.Text;
using Tracy.CoreLib.Data;

namespace CoreLib.Message
{
    public enum ActionType { Add, Replace, Delete, Update }
    public interface ITracyMessage
    {
        object Sender
        {
            get;
        }
        object Result
        {
            get;
            set;
        }

        Exception MessageException
        {
            get;
            set;
        }
    }

    public abstract class BasicTracyMessage : ITracyMessage
    {

        #region ITracyMessage Members
        protected object _sender;
        public virtual object Sender
        {
            get { return _sender; }
        }

        object _result;
        public virtual object Result
        {
            get
            {
                return _result;
            }
            set
            {
                _result = value;
            }
        }
        Exception _exception;
        public virtual Exception MessageException
        {
            get
            {
                return _exception;
            }
            set
            {
                _exception = value;
            }
        }

        #endregion

        public BasicTracyMessage(object sender)
        {
            _sender = sender;
        }
    }

    public class ExceptionMessage : BasicTracyMessage {

        public ExceptionMessage(object sender, Exception e)
            : base(sender)
        {
            MessageException = e;
        }
    }

    public class InitEngineMessage : BasicTracyMessage
    {
        public InitEngineMessage(object sender) : base(sender) { }
    }

    public class UpdateFeedMessage : BasicTracyMessage, ITracyMessage
    {
        public UpdateFeedMessage(object sender) : base(sender) { }
    }

    public class DownloadMessage : BasicTracyMessage
    {
        private ResourceItem _item;

        public ResourceItem Item
        {
            get { return _item; }
            set { _item = value; }
        }

        public DownloadMessage(object sender, ResourceItem item)
            : base(sender)
        {
            _item = item;
        }
    }

    public class TestFeedMessage : BasicTracyMessage
    {
        private string _link;

        public string Link
        {
            get { return _link; }
            set { _link = value; }
        }

        public TestFeedMessage(object sender, string link)
            : base(sender)
        {
            _link = link;
        }
    }

    public class AssignTagMessage : BasicTracyMessage
    {
        private TracyProperty _property;

        public TracyProperty Property
        {
            get { return _property; }
            set { _property = value; }
        }

        private IManagedXmlModelItem _source;

        public IManagedXmlModelItem Source
        {
            get { return _source; }
            set { _source = value; }
        }

        private IManagedXmlModelItem _target;

        public IManagedXmlModelItem Target
        {
            get { return _target; }
            set { _target = value; }
        }

        private TracyRelationship _relationship;

        public TracyRelationship Relationship
        {
            get { return _relationship; }
            set { _relationship = value; }
        }

        public AssignTagMessage(object sender, TracyProperty property, IManagedXmlModelItem source, IManagedXmlModelItem target)
            : base(sender)
        {
            _property = property;
            _source = source;
            _target = target;
        }
    }
    
    public class RemoveTagMessage : BasicTracyMessage
    {
        private IManagedXmlModelItem _source;

        public IManagedXmlModelItem Source
        {
            get { return _source; }
            set { _source = value; }
        }

        private IManagedXmlModelItem _target;

        public IManagedXmlModelItem Target
        {
            get { return _target; }
            set { _target = value; }
        }

        private TracyRelationship _relationship;

        public TracyRelationship Relationship
        {
            get { return _relationship; }
            set { _relationship = value; }
        }

        public RemoveTagMessage(object sender, IManagedXmlModelItem source, IManagedXmlModelItem target)
            : base(sender)
        {
            _source = source;
            _target = target;
        }

        public RemoveTagMessage(object sender, TracyRelationship relationship)
            : base(sender)
        {
            _relationship = relationship;
        }
    }

    public interface IManagedXmlModelMessage : ITracyMessage
    {
        ActionType Type
        {
            get;
        }

        IManagedXmlModelItem Item
        {
            get;
        }

    }

    #region ManagedXmlModelMessages
    public abstract class ManagedXmlModelMessage : BasicTracyMessage, IManagedXmlModelMessage
    {
        private ActionType _type;

        public virtual ActionType Type
        {
            get { return _type; }
        }

        private IManagedXmlModelItem _item;

        public IManagedXmlModelItem Item
        {
            get { return _item; }
        }

        public ManagedXmlModelMessage(object sender, ActionType type, IManagedXmlModelItem item):base(sender)
        {
            _type = type;
            _item = item;
        }
    }

    public class AddItemMessage : ManagedXmlModelMessage
    {
        public AddItemMessage(object sender, IManagedXmlModelItem item) : base(sender, ActionType.Add, item) { }
    }

    public class RemoveItemMessage : ManagedXmlModelMessage
    {
        public RemoveItemMessage(object sender, IManagedXmlModelItem item) : base(sender, ActionType.Delete, item) { }
    }

    public class ReplaceItemMessage : ManagedXmlModelMessage
    {
        private IManagedXmlModelItem _itemToReplace;

        public IManagedXmlModelItem ItemToReplace
        {
            get { return _itemToReplace; }
        }

        public ReplaceItemMessage(object sender, IManagedXmlModelItem newItem, IManagedXmlModelItem oldItem)
            : base(sender, ActionType.Replace, newItem) 
        {
            _itemToReplace = oldItem;
        }
    }

    public class UpdateItemMessage : ManagedXmlModelMessage
    {
        private string _key;

        public string Key
        {
            get { return _key; }
            set { _key = value; }
        }
        private string _value;

        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }

        public UpdateItemMessage(object sender, IManagedXmlModelItem item, string key, string value)
            : base(sender, ActionType.Update, item)
        {
            _key = key;
            _value = value;
        }
    }

    #endregion

    


}


